package com.example.hospitalmanagement

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.view.get
import androidx.core.view.isGone
import androidx.fragment.app.Fragment
import com.example.hospitalmanagement.fragment.HomeFragment
import com.example.hospitalmanagement.fragment.IndividualFragment
import com.example.hospitalmanagement.fragment.ServiceFragment
import kotlinx.android.synthetic.main.activity_main.*

/**
 * app 主页面
 *
 * 主页面UI显示以及Fragment的切换
 */
class MainActivity : AppCompatActivity() {
    private val fragments: List<Fragment> =
        listOf(ServiceFragment(), HomeFragment(), IndividualFragment())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        val fragmentManager = supportFragmentManager.beginTransaction()
        fragments.forEach { fragment ->
            fragmentManager.add(R.id.hospital_fragment, fragment).hide(fragment)
        }
        home_navi.menu[1].isChecked = true
        fragmentManager.show(fragments[1])
        fragmentManager.commit()
        home_navi.setOnNavigationItemSelectedListener {
            val fragmentManager = supportFragmentManager.beginTransaction()
            home_title.isGone = false

            fragments.forEach { fragment ->
                fragmentManager.hide(fragment)
            }
            when (it.itemId) {
                R.id.home_diagnosis_service -> fragmentManager.show(fragments[0])
                R.id.home_hospital_general -> fragmentManager.show(fragments[1])
                R.id.home_individual_center -> {
                    home_title.isGone = true
                    fragmentManager.show(fragments[2])
                }
            }
            fragmentManager.commit()
            true
        }
    }
}